package thread.useThread;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-03-04
 * Time:14:14
 */
public class useThread {
    /*
    Thread的start()方法调用start0()方法(是一个native方法)
    JVM机调用start0()后将线程转化为可运行状态
    最后通过 不同操作系统 + 算法 + 硬件标准 在线程内执行run()方法
     */
    public static void main(String[] args) {
        /*
        实现Runnable接口的Dog类并没有start()方法,不能直接开启线程
        解决方法:静态代理
                用Thread代理开启进程----Thread有构造方法 public Thread(Runnable task) { this(null, null, 0, task, 0, null); }
        静态代理条件:代理和被代理对象继承同一接口
                   在代理类中,被代理对象要初始化
         */
        Dog dog = new Dog();
        Thread thread = new Thread(dog);
        thread.start();
    }
    public static void main1(String[] args) throws InterruptedException {
        Cat cat = new Cat();
        cat.start();
        for (int i = 0; i < 6; i++) {
            System.out.println("main的数字" + (i + 1) + "  线程名:" + Thread.currentThread().getName());
            Thread.sleep(1000);
        }
    }
}


class Animal{
}
class Dog extends Animal implements Runnable{//实现Runnable接口创建线程(用于当前类已继承了其他类)
    @Override
    public void run() {
        int time = 0;
        while (true) {
            System.out.println("汪汪叫的小狗" + (++time) + "  线程名:" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (time == 8) {
                break;
            }
        }
    }
}

class Cat extends Thread {//继承Thread创建线程
    @Override
    public void run() {
        int time = 0;
        while (true) {
            System.out.println("喵喵叫的小猫" + (++time) + "  线程名:" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (time == 8) {
                break;
            }
        }
    }
}
